import commonservice from "./commonservice.js"


const uploadImage=function(filePath){
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url:commonservice.WEBSITE+"/api/_file/UploadImage",
			filePath:filePath,
			//files:filePath,//多图上传 wtm不支持
			name:'file',
			header: {
				'Authorization': 'Bearer ' +getApp().$store.state.accessToken
			 },
			// formData:{
			// 	'csName':'test'
			// },
			success:function(res){
				console.log(JSON.stringify(res))
				if (res.statusCode == 200 || res.statusCode == 400) {
					resolve(res)
				} else if (res.statusCode == 401) {
					getApp().$store.state.accessToken = '';
					getApp().$store.state.hasLogin=false;
					uni.showToast({
						title: '登录已失效',
						icon: 'none'
					});
					uni.navigateTo({
						url: '/pages/login/login'
					});  
				} else {
				
					uni.showToast({
						title: '状态码：' + res.statusCode,
						icon: 'none'
					});
				}
			},fail:function(ex){
				uni.showToast({
					title: '网络异常',
					icon:'none'
				});
			}
		})
	})
}

const uploadFile=function(filePath){
	return new Promise((resolve,reject)=>{
		uni.uploadFile({
			url:commonservice.WEBSITE+"/api/_file/Upload",
			filePath:filePath,
			name:'file',
			header: {
				'Authorization': 'Bearer ' + getApp().$store.state.accessToken
			},
			formData:{
				'csName':'test'
			},
			success:function(res){
				console.log(JSON.stringify(res))
				if (res.statusCode == 200 || res.statusCode == 400) {
					resolve(res)
				} else if (res.statusCode == 401) {
					getApp().$store.state.accessToken = '';
					getApp().$store.state.hasLogin=false;
					uni.showToast({
						title: '登录已失效',
						icon: 'none'
					});
					uni.navigateTo({
						url: '/pages/login/login'
					});  
				} else {
				
					uni.showToast({
						title: '状态码：' + res.statusCode,
						icon: 'none'
					});
				}
			},fail:function(ex){
				uni.showToast({
					title: '网络异常',
					icon:'none'
				});
			}
		})
	})
}

const GetFile=function(fileId){
		return new Promise((resolve,reject)=>{
			
			uni.downloadFile({
				url:commonservice.WEBSITE+"/api/_file/GetFile/"+fileId,
				header:{
					'Authorization':'Bearer '+commonservice.AccessToken
				},
				success:function(res){
					resolve(res)
				},
				fail:function(ex){
					uni.showToast({
						title: '网络异常',
						icon:'none'
					});
				}
			})
		})
}

export default{
	uploadImage:uploadImage,//上传图片
	uploadFile:uploadFile,  //上传文件
	GetFile:GetFile
};